package com.my.easymall.web;


import com.my.easymall.domain.User;
import com.my.easymall.exception.MsgException;
import com.my.easymall.service.UserService;

import com.my.easymall.utils.MD5Utils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @auther Summerday
 * <p>
 * 登录校验
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService service = new UserService();
        User user = new User();

        //获取用户信息,封装UserBean对象
        Map<String, String[]> map = request.getParameterMap();

        for (String[] value : map.values()) {
            if("".equals(value[0])){
                //如果不一致，提示用户名或密码错误
                request.setAttribute("login_error", "用户名或密码不能为空");
                //请求转发
                request.getRequestDispatcher("/login.jsp").forward(request, response);
                return;
            }
        }

        try {
            BeanUtils.populate(user, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        //用户登录记住用户名
        String remname = request.getParameter("remname");
        //获取用户是否选中30天内自动登录复选框
        String autologin = request.getParameter("autologin");
        String username = user.getUsername();
        String password = user.getPassword();
        if ("true".equals(remname)) {
            //如果rename的值为true，则记住用户名和remname的value
            Cookie username_cookie = new Cookie("remname", URLEncoder.encode(username, "utf-8"));

            username_cookie.setMaxAge(60 * 60 * 12);
            username_cookie.setPath(request.getContextPath() + "/");

            response.addCookie(username_cookie);

        } else {
            //销毁username_cookie
            Cookie username_cookie = new Cookie("remname", "kill");
            username_cookie.setMaxAge(0);
            username_cookie.setPath(request.getContextPath() + "/");
            response.addCookie(username_cookie);
        }

        //判断30天内是否自动登录
        if("true".equals(autologin)){

            //已勾选30天内自动登录
            Cookie cookie = new Cookie("autologin",URLEncoder.encode(username, "utf-8")+"#"+MD5Utils.md5(password));
            cookie.setMaxAge(60*60*24*30);
            cookie.setPath(request.getContextPath()+"/");
            response.addCookie(cookie);
        }


        User loginUser = null;
        //md加密算法
        user.setPassword(MD5Utils.md5(password));
        try {
            loginUser = service.loginUser(user);
        } catch (MsgException e) {
            //如果不一致，提示用户名或密码错误
            request.setAttribute("login_error", e.getMessage());
            //请求转发
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return;
        }
        //将用户的登录信息存储到session中
        HttpSession session = request.getSession();
        session.setAttribute("user", loginUser);
        //跳转回首页

        response.sendRedirect(request.getContextPath()+"/");


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
